/**
 * 合同下拉控件
 * @author 孙汉斌 2011-12-05
 * 特殊参数使用说明:
 *  1.providerid:为供应商ID.选出该供应商对应所有有效合同。可选项。若缺省则选出数据库所有合同(与供应商不再对应)
 */
package com.zjht.gmcc.bean.tag;

import java.sql.PreparedStatement;
import java.sql.ResultSet;

import javax.servlet.jsp.JspException;
import javax.servlet.jsp.tagext.TagSupport;

import com.zjht.gmcc.bean.util.MyConstant;
import com.zjht.gmcc.bean.util.MyFormat;
import com.zjht.gmcc.bean.util.MyJDBC;

public class SelectOrderTag extends TagSupport {
	private static final long serialVersionUID = 1L;
	private String name;
	private String id;
	private String firstoption;
	private String firstvalue;
	private String selected;
	private boolean disabled;
	private String providerid;

	public SelectOrderTag() {
	}

	@SuppressWarnings("static-access")
	public int doStartTag() throws JspException {
		String name = this.name;
		String id = this.id;
		String firstoption = this.firstoption;
		String firstvalue = this.firstvalue;
		String selected = this.selected;
		boolean disabled = this.disabled;
		String providerid=this.providerid;

		String sql = "";
		if (name == null || name.length() == 0) {
			name = "orderid";
		}
		if (id == null || id.length() == 0) {
			id = name;
		}

		StringBuilder html = new StringBuilder();
		html.append("<select name=\"");
		html.append(name);
		html.append("\"	id=\"");
		html.append(id);
		html.append("\" ");

		if (disabled) {
			html.append(" disabled");
		}
		html.append(">");
		if (!MyConstant.SELECT_FIRST_OPTION_NO.equals(firstoption))// 如果firstoption为MyConstant.SELECT_NO_FIRST_OPTION，则不需要添加额外的option
		{
			if (MyFormat.isStrNull(firstoption))
				firstoption = "全部";

			if (MyFormat.isStrNull(firstvalue))
				firstvalue = MyConstant.SELECT_FIRST_VALUE;

			html.append("<option value=\"");
			html.append(firstvalue);
			html.append("\">");
			html.append(firstoption);
			html.append("</option>");
		}
		MyJDBC myJDBC = null;
		try {
			myJDBC = new MyJDBC();
			sql = "SELECT orderid,ordercode FROM ORDERS WHERE STATUS<>'0' ";
			if(!MyFormat.isStrNull(providerid)){
				sql +=" AND providerid=?";
			}
			PreparedStatement ps = myJDBC.prepareStatement(sql);
			if(!MyFormat.isStrNull(providerid)){
				ps.setString(1, providerid);
			}
			ResultSet rs = ps.executeQuery();
			String s_orderid = "";
			while (rs.next()) {
				s_orderid = rs.getString("orderid");
				html.append("<option value=\"");
				html.append(s_orderid);
				html.append("\" ");
				if (selected != null && selected.length() > 0 && s_orderid.equals(selected))
					html.append("selected");
				html.append(">");
				html.append(rs.getString("ordercode"));
				html.append("</option>");
			}
			rs.close();
			rs = null;
			ps.close();
			ps = null;
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			MyJDBC.close(myJDBC);
		}
		html.append("</select>");
		try {
			pageContext.getOut().print(html.toString());
		} catch (Exception e) {
			throw new JspException(e.getMessage());
		}
		html = null;
		return this.SKIP_BODY;

	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public String getId() {
		return id;
	}

	public void setId(String id) {
		this.id = id;
	}

	public String getFirstoption() {
		return firstoption;
	}

	public void setFirstoption(String firstoption) {
		this.firstoption = firstoption;
	}

	public String getFirstvalue() {
		return firstvalue;
	}

	public void setFirstvalue(String firstvalue) {
		this.firstvalue = firstvalue;
	}

	public String getSelected() {
		return selected;
	}

	public void setSelected(String selected) {
		this.selected = selected;
	}

	public boolean isDisabled() {
		return disabled;
	}

	public void setDisabled(boolean disabled) {
		this.disabled = disabled;
	}

	public String getProviderid() {
		return providerid;
	}

	public void setProviderid(String providerid) {
		this.providerid = providerid;
	}
	

}
